﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SIMCFachadaNegocio;
using SIMCObjetoNegocio;

namespace SIMCPresenter
{
    public interface IConsultarUsuarioView : IViewBase
    {
        event EventHandler<SearchEventArgs<string>> Iniciar;
        string UserName { get; }
        void ConsultarUsuarioEnSesion();
        void ConsultarUsuarioEnBD();
    }

    public class ConsultarUsuarioPresenter : PresenterBase
    {
        private IConsultarUsuarioView mView;

        public ConsultarUsuarioPresenter(IConsultarUsuarioView view)
            : base(view)
        {
            this.mView = view;
            this.Initialize();
        }

        private void Initialize()
        {
            this.mView.Iniciar += new EventHandler<SearchEventArgs<string>>(mView_Iniciar);
        }

        private void mView_Iniciar(object sender, SearchEventArgs<string> e)
        {
            try
            {
                if (EsUsuarioEnSesion())
                {
                    mView.ConsultarUsuarioEnSesion();
                }
                else
                {
                    mView.ConsultarUsuarioEnBD();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                throw;
            }
        }

        private bool EsUsuarioEnSesion()
        {
            return mView.UserName == null;
        }
    }
}
